//
//  KSFormHItem.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/5/23.
//  Copyright © 2025 aowang. All rights reserved.
//  左右格式的表单 单个数据源

import UIKit

class KSFormHItem: KSFormBaseItem {
    
    override func disabledChange() {
        
        titleLabColorAdapt()
        textViewColorAdapt()
        if disabled {
        
            self.textView.placeHolder = ""
        } else {
             self.textView.placeHolder = self.appearance.placeholder
        }
     
        
        
        self.countV.isUserInteractionEnabled = !disabled
         self.segment.isUserInteractionEnabled = !disabled
        assistImgAdapt()
        layoutTextView()
    }
    override func actionClosureChange() {
        assistImgAdapt()
        if actionClosure.isSome {
             self.bgAddTap()
        }
         self.layoutTextView()
       
        configPlaceholder()
    }
 
    override  func  tempApddTextView() {
        
        self.addSubview(textView)
        textView.font = self.appearance.contentFont
        textView.plaAlignment = .right
        textView.textAlignment = .right
        textView.delegate = self
        textViewColorAdapt()
   
        layoutTextView()
    }
    // 【searchFlag图标和内容控件联合布局】
    override func layoutTextView() {
        guard textView.superview.isSome else {return}
        // 文本向左缩进的情况

      // 有点击事件 并且自动右箭头的时候
        // 有右边辅助图标时
        // 有右边辅助文字时
        // item可编辑时，有右侧手写笔图标

        
        var hasRight = false
        if !disabled {
            if (actionClosure.isSome && self.appearance.autoArrow) {
                hasRight = true
            }
            if self.appearance.assistImgStr.isSome {
                hasRight = true
            }
            if self.appearance.tailDesc.isSome {
                hasRight = true
            }
            if type == .tempTextView {
                hasRight = true
            }
        } else if appearance.tailDesc.isSome {
            hasRight = true
        }
        
//         var hasRight =  ((actionClosure.isSome && self.appearance.autoArrow) ||
//                         self.appearance.assistImgStr.isSome ||
//                         self.appearance.tailDesc.isSome  )
//      
//        if self.disabled {
//            hasRight = false
//        }
        
        textView.snp.remakeConstraints { make in
            if hasRight {
                make.right.equalTo(self).offset(-self.appearance.contentTrailing + 3)
                assistBtn.isHidden = false
            } else {
                make.right.equalTo(self).offset(-self.appearance.arrowTrailing)
                assistBtn.isHidden = true
            }
           
            make.left.equalTo(self.tpTitleLab.snp.right).offset(12)
            make.top.equalTo(self).offset(4)
            make.bottom.equalTo(self).offset(-3)
        }
    }
    
    override  func  addAssistImg() {
        
        self.addSubview(assistBtn)
        assistImgAdapt()
            assistBtn.snp.makeConstraints { make in
                make.right.equalTo(self).offset(-self.appearance.arrowTrailing)
                make.width.equalTo(18)
                make.height.equalTo(18)
                make.centerY.equalTo(self)
            }
        
        assistBtn.addTouchUpInSideAction { _ in
            if let clo = self.assistBtnAction {
                clo()
            } else if let col = self.actionClosure {
                col()
            }
        }
        
    }
    override func layoutTitle() {
        tpTitleLab.snp.makeConstraints { make in
            make.left.equalTo(self).offset(widgetLeading)
            make.centerY.equalTo(self)
            make.width.lessThanOrEqualTo(KSGlobal.screenW/2)
        }
    }
//    override func layoutArrow() {
//        arrowMark.snp.makeConstraints { make in
//            make.right.equalTo(self).offset(-self.appearance.arrowTrailing)
//            make.width.equalTo(18)
//            make.height.equalTo(18)
//            make.centerY.equalTo(self)
//        }
//    }
}
